# -*- coding: utf-8 -*-
import scrapy
import re
import json
from ..items import MiaoItem


class HuabanSpider(scrapy.Spider):
    name = 'huaban'
    allowed_domains = ['huaban.com']
    start_urls = ['https://huaban.com/partner/uc/aimeinv/pins/']

    def parse(self, response):
        # img_list = response.xpath('//script/text()').extract()
        imgArr = json.loads(re.findall('app.page\["pins"\] = (\[.+\])', response.body.decode('utf-8'))[0])
        pid = 0
        num = 0
        length = len(imgArr)
        for img in imgArr:
            num += 1
            if num == length:
                pid = img['pin_id']
            # 使用在items.py创建的数据模型item
            item = MiaoItem()
            # 拼接url得到完整的下载地址
            imgKey = img['file']['key']
            # # 将得到的下载地址，放入到数据模型中
            item['src'] = ['http://hbimg.huabanimg.com/{}'.format(imgKey)]
            print(pid)
            # # 将数据模型传输给管道
            yield item
            if pid:
                url = 'https://huaban.com/partner/uc/aimeinv/pins/?max={}&limit=8&wfl=1'.format(pid)
                #     # 将url传给scrapy.Request 得到的结果继续用self.parse处理
                yield scrapy.Request(url=url, callback=self.parse)
        pass
